﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Xml;
using System.Net;
using System.Text;
using System.Drawing;

namespace CMSLauncher
{
    public class PubClass
    {
        #region 字串格式化，数字格式化
        /// <summary>
        /// 格式化字串，当字串长度超过指定值后会被截断，含SQL敏感字符替换
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string SetStringLengthInterrupt(string str, int maxLength)
        {
            if (str.Length > maxLength)
                str = str.Substring(0, maxLength);
            str = str.Replace("'", "’");
            return str;
        }

        /// <summary>
        /// 格式化字串，当字串为空时使用默认字串，含SQL敏感字符替换
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string SetString(string str, string defaultValue)
        {
            if (str == "" || str == null)
            {
                str = defaultValue;
            }
            str = str.Replace("'", "’");
            return str;
        }

        /// <summary>
        /// 格式化数字字串，尝试转换十六进制为十进制，格式化或进制转换失败时返回"0"
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SetStringInt(string str)
        {
            if (str.Length > 2 && str.Substring(0, 2).ToLower() == "0x")
            {
                try
                {
                    str = Convert.ToInt32(str, 16).ToString();
                }
                catch
                {
                    str = "0";
                }
            }

            try
            {
                str = int.Parse(str).ToString();
            }
            catch
            {
                str = "0";
            }
            return str;
        }

        /// <summary>
        /// 格式化数字字串，格式化失败时返回默认值
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string SetStringInt(string str, string defaultValue)
        {
            try
            {
                str = int.Parse(str).ToString();
            }
            catch
            {
                str = defaultValue;
            }
            return str;
        }

        /// <summary>
        /// 格式化比特率，转换为Mbps(除以1000000)，精确到小数点后一位
        /// </summary>
        /// <param name="bitRate"></param>
        /// <returns></returns>
        public static string SetStringBitRate(object bitRate)
        {
            string result;
            double megaBitRate = 0;
            try
            {
                megaBitRate = Convert.ToDouble(bitRate) / 1000000;
            }
            catch { }
            if (Math.Floor(megaBitRate) == megaBitRate)
                result = megaBitRate.ToString("f0");
            else
                result = megaBitRate.ToString("f1");
            return result;
        }

        /// <summary>
        /// 强制转换为IP，若转换失败则返回0.0.0.0
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SetStringIpAddress(string str)
        {
            try
            {
                str = IPAddress.Parse(str).ToString();
            }
            catch
            {
                str = "0.0.0.0";
            }
            return str;
        }

        /// <summary>
        /// 验证字串是否是有效IP
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool CheckStringIpAddress(string ip)
        {
            try
            {
                if (ip == IPAddress.Parse(ip).ToString())
                    return true;
            }
            catch { }
            return false;
        }

        /// <summary>
        /// 验证数字字串是否是有效数字
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static bool CheckStringInt(string number)
        {
            bool result = false;
            try
            {
                int n = int.Parse(number);
                result = true;
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 验证数字字串是否是有效的数字或十六进制数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckStringIntHex(string str)
        {
            bool result = false;
            if (str.Length > 2 && str.Substring(0, 2).ToLower() == "0x")
            {
                try
                {
                    str = Convert.ToInt32(str, 16).ToString();
                }
                catch { }
            }

            try
            {
                str = int.Parse(str).ToString();
                result = true;
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 验证数字字串是否是在范围内
        /// </summary>
        /// <param name="number"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static bool CheckStringIntRange(string number, int min, int max)
        {
            bool result = false;
            try
            {
                int n = int.Parse(number);
                if (n >= min && n <= max)
                    result = true;
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 快速转换成十六进制
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string ConvertToHex(object value)
        {
            string result = "0x0";
            try
            {
                result = "0x" + Convert.ToString(Convert.ToInt32(value), 16).ToUpper();
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 快速转换数字
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int ConvertToInt32(object value, int defaultValue)
        {
            try
            {
                defaultValue = Convert.ToInt32(value);
            }
            catch { }
            return defaultValue;
        }

        /// <summary>
        /// 将数值型字串转换布尔(非0为true)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ConvertToBool(object value)
        {
            bool result = false;
            try
            {
                int temp = Convert.ToInt32(value);
                if (temp != 0)
                    result = true;
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 快速转换日期
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DateTime ConvertToDateTime(object value)
        {
            DateTime result = DateTime.Now;
            try
            {
                result = Convert.ToDateTime(value);
            }
            catch { }
            return result;
        }
        #endregion

        #region 日期文件大小等计算
        public static string GetStartWeekDay(string curWeek)
        {
            int nowDayOfWeek = (int)DateTime.Now.DayOfWeek;
            if (nowDayOfWeek == 0)
                nowDayOfWeek = 7;
            int dayShift = int.Parse(curWeek) * 7 - nowDayOfWeek + 1;
            return DateTime.Now.AddDays(dayShift).ToString("yyyy-MM-dd");
        }

        public static string GetEndWeekDay(string curWeek)
        {
            int nowDayOfWeek = (int)DateTime.Now.DayOfWeek;
            if (nowDayOfWeek == 0)
                nowDayOfWeek = 7;
            int dayShift = int.Parse(curWeek) * 7 - nowDayOfWeek + 1;
            return DateTime.Now.AddDays(dayShift + 7).ToString("yyyy-MM-dd");
        }

        public static string GetFileSizeFormatted(double nTotalFileSize)
        {
            string strTotalFileSize = "";

            int nKilos = 0;
            while (nTotalFileSize > 999)
            {
                nTotalFileSize /= 1024;
                nKilos++;
            }
            if (nTotalFileSize < 10)
                strTotalFileSize = nTotalFileSize.ToString("f2");
            else if (nTotalFileSize < 100)
                strTotalFileSize = nTotalFileSize.ToString("f1");
            else
                strTotalFileSize = nTotalFileSize.ToString("f0");

            switch (nKilos)
            {
                case 0:
                    strTotalFileSize += " Byte";
                    break;
                case 1:
                    strTotalFileSize += " KB";
                    break;
                case 2:
                    strTotalFileSize += " MB";
                    break;
                case 3:
                    strTotalFileSize += " GB";
                    break;
                case 4:
                    strTotalFileSize += " TB";
                    break;
                default:
                    strTotalFileSize = "Unknown";
                    break;
            }

            return strTotalFileSize;
        }

        public static string GetFileSizeFormatted(string fileSize)
        {
            double nTotalFileSize = 0;
            try
            {
                nTotalFileSize = Convert.ToDouble(fileSize);
            }
            catch { }

            return GetFileSizeFormatted(nTotalFileSize);
        }
        #endregion

        #region
        public static string GetLoginCipher()
        {
            // 生成600秒内有效的登录密钥
            return DateTime.Now.ToString("yyyyMMdd");
        }
        #endregion
    }
}